package com.atgouwu.scmall.product.service.impl;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson2.JSONObject; //fastjson2
import java.util.ArrayList; //ArrayList
import java.util.List; //List
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgouwu.scmall.common.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; //Page(删除Query)
import com.atgouwu.scmall.product.dao.AttrGroupDao;
import com.atgouwu.scmall.product.entity.AttrGroupEntity;
import com.atgouwu.scmall.product.service.AttrGroupService;

/**
 * 整合Mybatis-plus
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    //分页模糊检索
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if(params.get("name") != null && !"".equals(params.get("name"))) {
            queryWrapper.like("name", params.get("name")); //模糊检索
        }
        IPage<AttrGroupEntity> page = this.page( //分页
                new Page<>(Long.valueOf(String.valueOf(params.get("pageNum"))),Long.valueOf(String.valueOf(params.get("pageSize")))),
                queryWrapper
        );
        List<AttrGroupEntity> records = page.getRecords(); //解决long丢失精度,或修改id为非递增(用雪花算法)
        List<Object> vos = new ArrayList<>();
        for (AttrGroupEntity record : records) {
                AttrGroupEntity vo = JSONObject.parseObject(JSONObject.toJSONString(record), AttrGroupEntity.class);
            vos.add(vo);
        }
        //return new PageUtils(vos, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
        return new PageUtils(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    //queryWrapper参考
    //@Override
    public int reference(AttrGroupEntity entity) {
        //根据条件修改
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", entity.getAttrGroupId());
        //entity.setUpdateTime(new Date());
        int counta = baseMapper.update(entity, queryWrapper);
        //根据id修改
        int countb = baseMapper.updateById(entity);
        return counta;
    }

}